//
//  YQNetworkTools.m
//  WeiboOC
//
//  Created by yang on 15/11/19.
//  Copyright © 2015年 yang. All rights reserved.
//

#import "YQNetworkTools.h"

@implementation YQNetworkTools
+ (instancetype)sharedTools
{
    static YQNetworkTools* tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURL* url = [NSURL URLWithString:@"https://api.weibo.com/"];
        tools = [[self alloc] initWithBaseURL:url];
        tools.responseSerializer = [AFHTTPResponseSerializer serializer];

    });
    return tools;
}
- (void)requestJsonDict:(HTTPMethod)method urlString:(NSString*)urlString parameters:(NSDictionary*)parameters finished:(void (^)(id dict, NSError* error))finished
{
    if (method == POST) {
        [self POST:urlString
            parameters:parameters
            success:^(NSURLSessionDataTask* _Nonnull task, id _Nonnull responseObject) {
                if (responseObject) {
                    NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
                    finished(dict, nil);
                }
                else {
                    [SVProgressHUD showErrorWithStatus:@"数据错误"];
                }

            }
            failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
                finished(nil, error);
            }];
    }
    else {
        [self GET:urlString
            parameters:parameters
            success:^(NSURLSessionDataTask* _Nonnull task, id _Nonnull responseObject) {
                if (responseObject) {
                    NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
                    finished(dict, nil);
                }
                else {
                    [SVProgressHUD showErrorWithStatus:@"数据错误"];
                }
            }
            failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
                finished(nil, error);
            }];
    }
}
@end
